//
//  BlackListVC.swift
//  YueChat
//
//  Created by IMac on 2024/9/14.
//

import UIKit

class BlackListVC: BaseVC {

    override func viewDidLoad() {
        super.viewDidLoad()

        title = "黑名单"
        setup()
        self.BlackList()
    }


    func setup () {
        
        tableView.register(UINib(nibName: "UserCell", bundle: nil), forCellReuseIdentifier: "UserCell")
    }
    

    func BlackList () {
        let array  = gosnFriend.getBlackList() as NSArray
        dataSource = array.mutableCopy() as! [Any]
        DispatchQueue.main.async {
            self.tableView.reloadData()
        }
        
        if dataSource.count == 0 {
            self.view .insertSubview(self.nullView, aboveSubview: self.view)
        }else{
            self.nullView.removeFromSuperview()
        }

    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 70
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

       return 0.001
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "UserCell", for: indexPath) as! UserCell
        let frModel = dataSource[indexPath.row] as! DMCCFriend
        let user = gosnUser.getUserInfoDefault(frModel.friendId)
        cell.setUserInfo(userInfo: user)
        
        
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        tableView.deselectRow(at: indexPath, animated: true)
        
        if indexPath.row == 0 {
//            self.presentVC(VC: ForgotVC())
        }else if indexPath.row == 1 {
            
        }
    }
    
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        let action = UITableViewRowAction(style: .default, title: "移出") { [self] (action, indexPath) in
            // 删除操作的处理
            removeUserItem(at: indexPath)
        }
        return [action]
    }
    
    func removeUserItem(at indexPath: IndexPath) {
        guard let friend = dataSource[indexPath.row] as? DMCCFriend else { return }
        
        
        MHAlert.showLoading()
        
        Task{
                    let res = await gosnFriend.blackFriend(friend.userId, false)
                    await MainActor.run {
                        if res{
                            MHAlert.showMessage(KLocalizedString("AlertLoadSuccess"))
                            self.BlackList()
                        }else{
                            MHAlert.showMessage(KLocalizedString("AlertOpearFail"))
                        }
                    }
                }
    }
}
